﻿/*                              
   Copyright 2011 Nils Kopal, Christopher Krüger, Simone Sauer, Dennis Nolte, Uni Duisburg-Essen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Numerics;

namespace Sharebert.OverlayLibrary
{

    /// <summary>
    /// Represents a PeerID. This class is a BigInteger wrapper, which is neccessary because System.Numerics.BigInteger
    /// can not be serialized.
    /// </summary>
    [Serializable]
    public class PeerID : IComparable<PeerID>
    {

        //public const int PEER_ID_BYTE_SIZE = 20;

        // For testing
        public const int PEER_ID_BYTE_SIZE = 2;

        public static readonly BigInteger RING_SIZE = BigInteger.Pow(new BigInteger(2), PEER_ID_BYTE_SIZE * 8);


        public PeerID()
        {
        }

        public PeerID(byte[] peerId)
            : this(new BigInteger(peerId))
        { }

        public PeerID(BigInteger peerId)
        {
            if (peerId < BigInteger.Zero)
            {
                throw new ArgumentNullException("peerId must be positiv!");
            }
            this.BigIntegerValue = peerId;
        }

        public static bool operator <(PeerID p1, PeerID p2)
        {
            return p1.CompareTo(p2) < 0;
        }

        public static bool operator <=(PeerID p1, PeerID p2)
        {
            return p1.CompareTo(p2) <= 0;
        }

        public static bool operator >(PeerID p1, PeerID p2)
        {
            return p1.CompareTo(p2) > 0;
        }

        public static bool operator >=(PeerID p1, PeerID p2)
        {
            return p1.CompareTo(p2) >= 0;
        }

        public static bool operator ==(PeerID p1, PeerID p2)
        {
            return p1.Equals(p2);
        }

        public static bool operator !=(PeerID p1, PeerID p2)
        {
            return !(p1.Equals(p2));
        }

        public int CompareTo(PeerID other)
        {
            return this.BigIntegerValue.CompareTo(other.BigIntegerValue);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            PeerID other = (PeerID)obj;
            return this.BigIntegerValue.Equals(other.BigIntegerValue);
        }

        public override int GetHashCode()
        {
            return this.BigIntegerValue.GetHashCode();
        }

        public override string ToString()
        {
            return this.BigIntegerValue.ToString();
        }

        public static PeerID GetRandomPeerID()
        {
            // BigInteger class has no unsigned values, so we use this workaround
            var tmp = new byte[PEER_ID_BYTE_SIZE];
            Random rnd = new Random();
            rnd.NextBytes(tmp);
            BigInteger id = new BigInteger(tmp);
            if (id < 0)
            {
                id = id + RING_SIZE;
            }
            return new PeerID(id);
        }

        public BigInteger BigIntegerValue 
        {
            get
            {
                if (this.StringValue != null && !String.IsNullOrEmpty(this.StringValue))
                {
                    BigInteger bi = new BigInteger();
                    if (BigInteger.TryParse(this.StringValue, out bi))
                    {
                        return bi;
                    }
                }
                return BigInteger.MinusOne;
            }
                
            set { this.StringValue = value.ToString(); }
        }

        public String StringValue { get; set; }
    }
}
